package com.whzykj.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Random;

public class DateUtil {
	private static String datePattern = "yyyy-MM-dd";
	private static String timePattern = "HH:mm";
	private static String secondPattern = "HH:mm:ss";
	/** 默认的格式化方式 */
	private static final String defaultFormat = "yyyy-MM-dd HH:mm:ss";
	
	/**
	 * 获取当前的日期字符串，格式为：yyyy-MM-dd HH:mm:ss，功能等同于：getNow()
	 * @return
	 */
	public static String getDate() {
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		Date currentDate = new Date();
		String formatCurrentDate = dateFormat.format(currentDate).toString();

		return formatCurrentDate;
	}
	
	public static String getDateStr(String s) {
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddhhmmss");
		Date strtodate=null;
		try {
			strtodate = sdf1.parse(s);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		return sdf2.format(strtodate);
	}
	
	public static String getDateByStr(String s) {
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date strtodate=null;
		try {
			strtodate = sdf1.parse(s);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMddhhmmss"); 
		return sdf2.format(strtodate);
	}
	
	/**
	 * 获取当前的日期字符串，格式为：yyyy-MM-dd，功能等同于getToday()
	 * @return
	 */
	public static String getCurrentDate() {
		String format = "yyyy-MM-dd";
		Date date = new Date();
		date.setTime(System.currentTimeMillis());
		if (format == null || "".equals(format.trim())) {
			format = defaultFormat;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}

	/**
	 * 获取当前的日期字符串，格式为：yyyyMMddHHmmss
	 * @return
	 */
	public static String getCurrentTime() {
		String format = "yyyyMMddHHmmss";
		Date date = new Date();
		date.setTime(System.currentTimeMillis());
		if (format == null || "".equals(format.trim())) {
			format = defaultFormat;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}

	public static String getDatePattern() {
		return datePattern;
	}

	public static final String getDate(Date aDate) {
		SimpleDateFormat df = null;
		String returnValue = "";

		if (aDate != null) {
			df = new SimpleDateFormat(datePattern);
			returnValue = df.format(aDate);
		}

		return (returnValue);
	}
	
	/**
	 * 将指定格式的日期字符串转换为Date对象，格式举例：yyyy-MM-dd HH:mm:ss、yyyy-MM-dd等。
	 * @param aMask
	 * @param strDate
	 * @return
	 * @throws ParseException
	 */
	public static final Date convertStringToDate(String aMask, String strDate)
			throws ParseException {
		SimpleDateFormat df = null;
		Date date = null;
		df = new SimpleDateFormat(aMask);

		try {
			date = df.parse(strDate);
		} catch (ParseException pe) {
			// log.error("ParseException: " + pe);
			throw new ParseException(pe.getMessage(), pe.getErrorOffset());
		}

		return (date);
	}

	/**
	 * 将2016-09-01 12:11:12格式的字符串转换为：日期类型
	 * 
	 * @param strDate
	 * @return
	 */
	public static final Date convertFullStringToDate(String strDate) {
		SimpleDateFormat df = null;
		Date date = null;
		String aMask = "yyyy-MM-dd HH:mm:ss";
		df = new SimpleDateFormat(aMask);

		try {
			date = df.parse(strDate);
		} catch (ParseException pe) {
			pe.printStackTrace();
		}

		return (date);
	}

	/**
	 * This method returns the current date time in the format: yyyy/MM/dd HH:MM
	 * a
	 * 
	 * @param theTime
	 *            the current time
	 * @return the current date/time
	 */
	public static String getTimeNow(Date theTime) {
		// return getDateTime(timePattern, theTime);
		return getDateTime(secondPattern, theTime);
	}

	/**
	 * This method returns the current date in the format: yyyy/MM/dd
	 * 
	 * @return the current date
	 * @throws ParseException
	 */
	public static Calendar getCalendarToday() throws ParseException {
		Date today = new Date();
		SimpleDateFormat df = new SimpleDateFormat(datePattern);

		// This seems like quite a hack (date -> string -> date),
		// but it works ;-)
		String todayAsString = df.format(today);
		Calendar cal = new GregorianCalendar();
		cal.setTime(convertStringToDate(todayAsString));

		return cal;
	}

	/**
	 * This method generates a string representation of a date's date/time in
	 * the format you specify on input
	 * 
	 * @param aMask
	 *            the date pattern the string is in
	 * @param aDate
	 *            a date object
	 * @return a formatted string representation of the date
	 * 
	 * @see java.text.SimpleDateFormat
	 */
	public static final String getDateTime(String aMask, Date aDate) {
		SimpleDateFormat df = null;
		String returnValue = "";

		if (aDate == null) {
			// log.error("aDate is null!");
		} else {
			df = new SimpleDateFormat(aMask);
			returnValue = df.format(aDate);
		}

		return (returnValue);
	}

	public static final String convertDateToString(Date aDate) {
		return getDateTime(datePattern, aDate);
	}

	public static Date convertStringToDate(String strDate)
			throws ParseException {
		Date aDate = null;
		try {

			aDate = convertStringToDate(datePattern, strDate);
		} catch (ParseException pe) {
			pe.printStackTrace();
		}
		return aDate;
	}
	
	/**
	 * 返回java.sql.Date对应的日期字符串，格式为：yyyy-MM-dd
	 * @param aDate
	 * @return
	 */
	public static String convertDateToString(java.sql.Date sqlDate){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return df.format(new java.util.Date(sqlDate.getTime()));
	}

	/**
	 * 获取现在的日期时间字符串，格式为：yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String getNow() {
		return DateUtil.getDateTime("yyyy-MM-dd HH:mm:ss", new Date());
	}
	
	/**
	 * 获取今天的日期字符串，格式为：yyyy-MM-dd
	 * @return
	 */
	public static String getToday() {
		return DateUtil.getDateTime("yyyy-MM-dd", new Date());
	}
	
	/**
	 * 获取昨天的日期字符串，格式为：yyyy-MM-dd
	 * @return
	 */
	public static String getYesterday(){
		long time=new java.util.Date().getTime();
		long yesterdayTime=time-24*60*60*1000;
		Date yesterday=new Date(yesterdayTime);
		return DateUtil.getDateTime("yyyy-MM-dd", yesterday);
	}
	
	/**
	 * 获取明天的日期字符串，格式为：yyyy-MM-dd
	 * @return
	 */
	public static String getTomorrow(){
		long time=new java.util.Date().getTime();
		long yesterdayTime=time+24*60*60*1000;
		Date yesterday=new Date(yesterdayTime);
		return DateUtil.getDateTime("yyyy-MM-dd", yesterday);
	}
	
	/**
	 * 获取本周第一天的日期字符串，格式为：yyyy-MM-dd
	 * @return
	 */
	public static String getCurrentWeekFirstDay(){
		Calendar cal =Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return df.format(cal.getTime());
	}
	
	/**
	 * 获取本周最后一天的日期字符串，格式为：yyyy-MM-dd
	 * @return
	 */
	public static String getCurrentWeekLastDay(){
		Calendar cal =Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        cal.add(Calendar.WEEK_OF_YEAR, 1);
        return df.format(cal.getTime());
	}
	
	/**
	 * 获取上周第一天的日期字符串，格式为：yyyy-MM-dd
	 * @return
	 */
	public static String getLastWeekFirstDay(){
		Calendar cal =Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cal.add(Calendar.WEEK_OF_YEAR, -1);
        return df.format(cal.getTime());
	}
	
	/**
	 * 获取上周最后一天的日期字符串，格式为：yyyy-MM-dd
	 * @return
	 */
	public static String getLastWeekLastDay(){
		Calendar cal =Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        return df.format(cal.getTime());
	}
	
	/**
	 * 获取本月第一天的日期字符串，格式为：yyyy-MM-dd
	 * @return
	 */
	public static String getCurrentMonthFirstDay(){
		Calendar cal =Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        cal.set(Calendar.DATE, cal.getActualMinimum(Calendar.DATE));       
        return df.format(cal.getTime());
	}
	
	/**
	 * 获取本月最后一天的日期字符串，格式为：yyyy-MM-dd
	 * @return
	 */
	public static String getCurrentMonthLastDay(){
		Calendar cal =Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        cal.set(Calendar.DATE, cal.getActualMaximum(Calendar.DATE));
        return df.format(cal.getTime());
	}
	
	/**
	 * 获取上月第一天的日期字符串，格式为：yyyy-MM-dd
	 * @return
	 */
	public static String getLastMonthFirstDay(){
		Calendar cal =Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        cal.set(Calendar.DATE, cal.getActualMinimum(Calendar.DATE)); 
        cal.add(Calendar.MONTH, -1);
        return df.format(cal.getTime());
	}
	
	/**
	 * 获取上月最后一天的日期字符串，格式为：yyyy-MM-dd
	 * @return
	 */
	public static String getLastMonthLastDay(){
		Calendar cal =Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        cal.set(Calendar.DATE, cal.getActualMaximum(Calendar.DATE));
        cal.add(Calendar.MONTH, -1);
        return df.format(cal.getTime());
	}

	public static void main(String[] args) {
//		System.out.println(DateUtil.getDateTime("yyyy-MM-dd HH:mm:ss",
//				new Date()));
		System.out.println(getDateStr("20141030133525"));
	}

	/**
	 * 判断是否超过1小时
	 * 
	 * @param date1
	 * @param date2
	 * @return boolean
	 * @throws Exception
	 */
	public static boolean judgmentDate(String date1, String date2)
			throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d HH:mm:ss");
		Date start = sdf.parse(date1);
		Date end = sdf.parse(date2);
		long cha = end.getTime() - start.getTime();
		if (cha < 0) {
			return false;
		}
		String order_close_time = SystemRunParamCache.cache
				.getParamValue("order_close_time"); // 从缓存中获取订单自动关闭时间（时间单位：分钟）
		int close_time = 30; // 默认关闭时间是60分钟
		if (order_close_time != null && !order_close_time.equals("")) {
			try {
				close_time = Integer.valueOf(order_close_time); // 获取系统配置中的订单自动关闭的时间
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				close_time = 30;
			}
		}
		// double result = cha * 1.0 / (1000 * 60 * 60);
		double result = cha * 1.0 / (1000 * 60); // 获取当前时间和下单时间的间隔分钟数
		if (result < close_time) {
			return true;
		} else {
			return false;
		}
	}
}
